package com.gjintel.framework.dict.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.gjintel.framework.dict.dao.IDictDao;
import com.gjintel.framework.dict.model.Dictionary;
import com.gjintel.sdk.base.dao.HibernateGenericDAO;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.util.StringUtil;

/**
 * @description 数据字典数据访问的实现
 * @author fengya
 * @date 2015-8-12 上午11:23:25
 */
@Repository("dictDao")
public class DictDaoImpl extends HibernateGenericDAO implements IDictDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Dictionary> findAll() {
		String hql = "from Dictionary d where d.flag=0 order by d.disSort ASC";
		return this.find(hql);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void findAll(Page page, Dictionary dict, Sort sort) {
		StringBuffer hql = new StringBuffer("from Dictionary d where d.flag=0 and d.id != '1'");
		List<String> params = new ArrayList<String>(2);
		if (dict != null) {
			if (!StringUtil.isEmpty(dict.getName())) {
				hql.append(" and d.name like ? ");
				params.add("%" + dict.getName().trim() + "%");
			}
			if (!StringUtil.isEmpty(dict.getPid())) {
				hql.append(" and d.pid=? ");
				params.add(dict.getPid().trim());
			}
		}
		hql.append(" order by d.disSort ASC");
		this.findAsPage(hql.toString(), page, sort, "d", false, params.toArray());
	}

}
